/*
 * watchdog.c
 *
 *  Created on: Sep 5, 2013
 *      Author: Ken Arok
 *
 *      Revision history:
 *      ---------------------------------------------------------
 *      Date			|	Revised by		|	Description
 *      ---------------------------------------------------------
 *      1. Sep 5, 2013	|	Yosef			| New establishment
 *
 *
 * \file
 *
 * \brief Watchdog Timer Library.
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */

#include "wdt.h"
#include "watchdog.h"

// Min value of 1s
#define WDT_MIN_VALUE_US   1000000
// Min value of 4s
#define WDT_MAX_VALUE_US   4000000
// Step of 1s
#define WDT_CTRL_STEP_US   1000000

static wdt_opt_t opts = {
	.dar   = true,							// After a watchdog reset, the WDT will still be disabled.
	.mode  = WDT_BASIC_MODE,				// The WDT is in basic mode, only PSEL time is used.
	.sfv   = false,     					// WDT Control Register is not locked.
	.fcd   = false,     					// The flash calibration will be redone after a watchdog reset.
	.cssel = WDT_CLOCK_SOURCE_SELECT_RCSYS,	// Select the system RC oscillator (RCSYS) as clock source.
	.us_timeout_period = WDT_MAX_VALUE_US	// Timeout Value
};

void vWatchdogTimerCtrl(watchdog_timer_ctrl_t _ctrl, void *_pValue)
{
	switch(_ctrl) {
		case WDT_GET_TIMEOUT_MS:
			*(uint64_t *)_pValue = wdt_get_us_timeout_period(&opts);
		break;

		case WDT_GET_TIMEBAN:
			*(uint64_t *)_pValue = wdt_get_us_timeban_period(&opts);
		break;

		case WDT_CLEAR:
			wdt_clear();
		break;

		case WDT_RESET_MCU:
			wdt_reset_mcu();
		break;

		case WDT_ENABLE:
			wdt_enable(&opts);
		break;

		case WDT_REENABLE:
			wdt_reenable();
		break;

		case WDT_DISABLE:
			wdt_disable();
		break;

		case WDT_SET_TIMEOUT:
			opts.us_timeout_period = *(uint64_t *)_pValue;
		break;

		case WDT_SET_MODE:
			opts.mode = *(uint8_t *)_pValue;
		break;

		case WDT_DISABLE_AFTER_RESET:
			opts.dar = *(uint8_t *)_pValue;
		break;

		case WDT_STORE_FINAL_VALUE:
			opts.sfv = *(uint8_t *)_pValue;
		break;

		default: break;
	}
}


